/**
 * file: dbHelper
 * description:  数据库连接管理类 
 * author : [" t_fate@163.com "]
 * date : 2014/3/7
 */
'use strict';
var mongoose = require('mongoose');
var mysql = require('mysql');

var dbHelper = function(){}

/**
 * name: connectMongo
 * description: 连接到mongodb
 * author : [" t_fate@163.com "]
 * date : 2014/3/7
 */
dbHelper.prototype.connectMongo = function(options, callback){
      //组装DBURI
      //'mongodb://user:pass@localhost:port/database'
      var dbUri = 'mongodb://'+options.username+':'+options.password+'@'+options.db_host+':'+options.db_port+'/'+options.db_name;
      // 
      var connection = mongoose.createConnection(dbUri,{
          server : {
              poolSize : options.poolsize
          }
      }); 
      
      // 数据库连接失败
      connection.on('error',function(err){
           console.log('mongodb connect error : [' + err + ']');
           !!callback && callback(err);
      });
      
      // 数据库连接成功
      connection.once('open',function(){
           console.log('mongodb connect success! port : ' + options.db_port);
           if(!!callback){
              callback(null, connection);    
           }else{
              return connection;
           }
      });
}

/**
 * name: connectMysql
 * description: 连接到Mysql
 * author : [" t_fate@163.com "]
 * date : 2014/3/7
 */
dbHelper.prototype.connectMysql = function(options, callback){
      //var connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700');
      
      var connection = mysql.createConnection({
          host : options.db_host,
          port : options.db_port,
          user : options.username,
          password : options.password,
          database : options.db_name,
          charset : 'UTF8_GENERAL_CI'
      });

      connection.connect(function(err){
          if(!!err){
              !!callback && callback(err);
              console.log(callback);
              return false;
          }

          if(!!callback){
              callback( null , connection);    
          }else{
              return connection;
          }
          console.log('mysql connect success! port : '+ options.db_port);
      }); 

      // 关闭链接
      //connection.end(function(err){console.log(err);});
      //or
      //connection.destroy();

      
}

/**
 * name: createMysqlPool
 * description: 建立mysql连接池   
 * author : [" t_fate@163.com "]
 * date : 2014/3/7
 */
dbHelper.prototype.createMysqlPool = function(options, callback){
   
    var pool = mysql.createPool({
        host : options.db_host,
        port : options.db_port,
        user : options.username,
        password : options.password,
        database : options.db_name,
        charset : 'UTF8_GENERAL_CI',
        connectionLimit : options.poolsize
    });

    if(!!callback){
          callback(pool);    
    }else{
          return pool;
    } 
}

module.exports = dbHelper;